layui.use(['form','layer','jquery','laypage'],function(){
	var form = layui.form(),
		layer = parent.layer === undefined ? layui.layer : parent.layer,
		laypage = layui.laypage,
		$ = layui.jquery;

	//添加权限
	$(".ruleAdd_btn").click(function(){
		var index = layui.layer.open({
			title : "添加权限",
			type : 2,
			content : "/addRule",
			success : function(layero, index){
				setTimeout(function(){
					layui.layer.tips('点击此处返回权限列表', '.layui-layer-setwin .layui-layer-close', {
						tips: 3
					});
				},500)
			}
		})
		//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
		$(window).resize(function(){
			layui.layer.full(index);
		})
		layui.layer.full(index);
	})

	//编辑权限
	$("body").on("click",".rule_edit",function(){  //编辑
		var id = $(this).attr('data-id');
		var index = layui.layer.open({
			title : "编辑权限",
			type : 2,
			content : "/editRule/"+id,
			success : function(layero, index){
				setTimeout(function(){
					layui.layer.tips('点击此处返回权限列表', '.layui-layer-setwin .layui-layer-close', {
						tips: 3
					});
				},500)
			}
		})
		//改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
		$(window).resize(function(){
			layui.layer.full(index);
		})
		layui.layer.full(index);
	})

	$("body").on("click",".rule_del",function(){  //删除
		var _this = $(this);
		var id = $(this).attr('data-id');
		layer.confirm('确定删除此信息？',{icon:3, title:'提示信息'},function(index){
			layer.close(index);
			$.ajax({
				url: '/deleteRule',
				type: 'POST',
				dataType: 'json',
				timeout: 3000,
				data: {id: id},
				complete: function(){},
				error: function(){
					layer.alert('参数错误', {icon: 2}, function(index){
						layer.close(index);
					});
				},
				success: function(res){
					if (res.status != 0) {
						layer.msg(res.msg);
					} else {
						layer.msg("删除成功");
						_this.parents("tr").remove();
					}
				}
			});
		});
	})

	form.on('switch(isCheck)', function(data){
		var id = $(this).attr('data-id');
		var orig = $(this).prop('checked');
		var check;
		if (orig) {
			check = 1;
		} else {
			check = 0;
		}
		var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
		$.ajax({
			url: '/editCheckRule',
			type: 'POST',
			dataType: 'json',
			timeout: 2000,
			data: {id: id, check: check},
			complete: function(){
				layer.close(index);
			},
			error: function(){
				layer.alert('参数错误', {icon: 2}, function(index){
					layer.close(index);
					// location.reload();
				});
			},
			success: function(res){
				if (res.status != 0) {
					layer.msg(res.msg);
					// location.reload();
				} else {
					layer.msg("已更改成功");
				}
			}
		});
		return false;
	})

	form.on('switch(isShow)', function(data){
		var id = $(this).attr('data-id');
		var orig = $(this).prop('checked');
		var status;
		if (orig) {
			status = 1;
		} else {
			status = 0;
		}
		var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
		$.ajax({
			url: '/editShowRule',
			type: 'POST',
			dataType: 'json',
			timeout: 2000,
			data: {id: id, status: status},
			complete: function(){
				layer.close(index);
			},
			error: function(){
				layer.alert('参数错误', {icon: 2}, function(index){
					layer.close(index);
					location.reload();
				});
			},
			success: function(res){
				if (res.status != 0) {
					layer.msg(res.msg);
					location.reload();
				} else {
					layer.msg("已更改成功");
				}
			}
		});
		return false;
	})

	$('.sort_input').change(function(){
		var id = $(this).attr('data-id');
		var sort = $(this).val();
		sort = Number(sort);
		var index = layer.msg('修改中，请稍候',{icon: 16,time:false,shade:0.8});
		$.ajax({
			url: '/editSortRule',
			type: 'POST',
			dataType: 'json',
			timeout: 2000,
			data: {id: id, sort: sort},
			complete: function(){
				layer.close(index);
			},
			error: function(){
				layer.alert('参数错误', {icon: 2}, function(index){
					layer.close(index);
					location.reload();
				});
			},
			success: function(res){
				if (res.status != 0) {
					layer.msg(res.msg);
					location.reload();
				} else {
					layer.msg("已更改成功");
				}
			}
		});
		return false;
	});

})
